package com.dikang.keer.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.dikang.common.utils.PageUtils;
import com.dikang.common.utils.Query;
import com.dikang.keer.product.entity.SpuInfoEntity;
import com.dikang.keer.product.vo.SpuSaveVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.dikang.keer.product.dao.SkuInfoDao;
import com.dikang.keer.product.entity.SkuInfoEntity;
import com.dikang.keer.product.service.SkuInfoService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void saveSKuInfo(SkuInfoEntity skuInfoList) {
        this.save(skuInfoList);
    }
    @Override
    public PageUtils getSkuInfoPage(Map<String, Object> params) {
    System.out.println(params);
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.and((w)->{
                wrapper.eq("id",key).or().like("spu_name",key);
            });
        }
        String status = (String) params.get("status");
        if (!StringUtils.isEmpty(status)){
            wrapper.eq("publish_status",status);
        }
        String brandId = (String) params.get("brandId");
        if (!brandId.equals("0")){
            wrapper.eq("brand_id",brandId);
        }
        String catelogId = (String) params.get("catelogId");
        if (!catelogId.equals("0")){
            // 这个地方数据库是catalog_id 为了契合数据库所以这样
            wrapper.eq("catalog_id",catelogId);
        }
        IPage<SkuInfoEntity> page = new Query<SkuInfoEntity>().getPage(params);
        IPage<SkuInfoEntity> Ipage = this.page(page, wrapper);
        return new PageUtils(Ipage);
    }


}